import sys
import os


def bin_to_mem(infile, outfile):
    binfile = open(infile, 'rb')
    binfile_content = binfile.read(os.path.getsize(infile))
    datafile = open(outfile, 'w')
    datafile.write("""WIDTH=8;
DEPTH=8192;

ADDRESS_RADIX=UNS;
DATA_RADIX=UNS;

CONTENT BEGIN
""")
    b0 = 0
    i=0
    for b in  binfile_content:
        b0 = b
        array = []
        array.append(b0)
        datafile.write( "\t" + str(i) +"\t\t:" + "\t" + str(int(bytearray(array).hex(),16)) + ";\n")
        i=i+1
        
    if (i < 8192):
        datafile.write("\t["+ str(i) +"..8191]\t:\t0;\n")
    datafile.write("END;")
    binfile.close()
    datafile.close()


if __name__ == '__main__':
    if len(sys.argv) == 3:
        bin_to_mem(sys.argv[1], sys.argv[2])
    else:
        print('Usage: %s binfile datafile' % sys.argv[0], sys.argv[1], sys.argv[2])
